import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:sports_new/entity/other/error_info_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/sp_utils.dart';
import 'package:sports_new/utils/toast_utils.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/widgets/card.dart';
import 'package:sports_new/widgets/base/qkd_button.dart';
import 'package:sports_new/widgets/base/qkd_dialog.dart';

import '../../widgets/base/qkd_text.dart';

class ErrorPage extends StatefulWidget {
  const ErrorPage({super.key});

  @override
  State<ErrorPage> createState() => _ErrorPageState();
}

class _ErrorPageState extends State<ErrorPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colours.greyF2F3F7,
      appBar: Styles.appBar(
        title: QkdText("错误列表(${SpUtils.errorInfo?.length?? 0})"),
        actions: [
          IconButton(
            onPressed: () {
              SpUtils.showErrorToast = !SpUtils.showErrorToast;
              ToastUtils.showDismiss(SpUtils.showErrorToast?"打开错误提示":"关闭错误提示");
              setState(() {});
            },
            icon: Icon(SpUtils.showErrorToast?Icons.radio_button_checked:Icons.radio_button_off)
          ),
          IconButton(
            onPressed: () async{
              var result = await Get.dialog(QkdDialog.alert("要删除所有记录吗？"));
              if(result == true) SpUtils.deleteErrorInfo();
              setState(() {});
            },
            icon: const Icon(Icons.delete)
          )
        ]
      ),
      body: ListView(
        children: SpUtils.errorInfo?.reversed.map(
          (e) => errorItem(ErrorInfoEntity.fromJson(jsonDecode(e)))
        ).toList().append(Container(height: 50)) ?? []
      ),
    );
  }

  Widget errorItem(ErrorInfoEntity entity){
    return Block(
      cardStyle: const CardStyle(
        margin: EdgeInsets.fromLTRB(8, 8, 8, 0),
        padding: EdgeInsets.symmetric(horizontal: 10,vertical: 6)
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          if(entity.path.hasValue)
          QkdText("路径：${entity.path ?? ""}"),
          Container(height: 6),
          if(entity.stack.hasValue)
          QkdText("堆栈：${entity.stack ?? ""}"),
          Container(height: 6),
          QkdText(
            "报错: ${entity.exception ?? " "}",
            maxLines: 5,
            color: Colours.grey66
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              QkdText(entity.time ?? ""),
              QkdButton(
                textStyle: const TextStyle(fontSize: 12,fontWeight: FontWeight.w400),
                backgroundColor: Colours.main,
                foregroundColor: Colours.white,
                borderRadius: 4,
                height: 24,
                padding: const EdgeInsets.symmetric(horizontal: 4,vertical: 4),
                onPressed: () {
                  Clipboard.setData(ClipboardData(text: entity.toString()));
                  ToastUtils.showDismiss("已复制");
                },
                text: "复制到剪贴板",
              )
            ],
          ).paddingOnly(top: 4)
        ],
      ),
    );
  }
}
